﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.BLL;
using PBOffice.Models;

public partial class NewsManager_AddNews : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.ddlNewsType.DataSource = new NewsTypeManager().GetAllType();
            this.ddlNewsType.DataTextField = "TypeName";
            this.ddlNewsType.DataValueField = "TypeId";
            this.ddlNewsType.DataBind();
            ListItem ll = new ListItem("请选择");
            this.ddlNewsType.Items.Insert(0, ll);
            if (Request.QueryString["id"] != null)
            {
                int newsId = Convert.ToInt32(Request.QueryString["id"]);
                News news = new NewsManager().GetNewsById(newsId);
                this.txtTitle.Text = news.NewsTitle;
                this.ddlNewsType.Text = news.NewsType.TypeName;
                this.FCKeditor1.Value = news.NewsContent;
                if (news.IsTop)
                {
                    this.rbTopYes.Checked = true;
                }
                else
                {
                    this.rbTopNo.Checked = true;
                }
                if (news.IsCommend)
                {
                    this.rbYes.Checked = true;
                }
                else
                {
                    this.rbNo.Checked = true;
                }
            }

        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (CheckEmpty())
        {
            News news = new News();
            if (this.rbYes.Checked == true)
            {
                news.IsCommend = true;
            }
            if (this.rbNo.Checked == true)
            {
                news.IsCommend = false;
            }
            if (this.rbTopYes.Checked == true)
            {
                news.IsTop = true;
            }
            if (this.rbTopNo.Checked == true)
            {
                news.IsTop = false;
            }
            news.NewsContent = this.FCKeditor1.Value;
            news.NewsTitle = this.txtTitle.Text.Trim();
            NewsType type = new NewsType();
            type.TypeId = Convert.ToInt32(this.ddlNewsType.SelectedItem.Value);
            type.TypeName = this.ddlNewsType.SelectedItem.Text;
            news.NewsType= type;
            if (Request.QueryString["id"] != null)
            {
                News nnn = new NewsManager().GetNewsById(Convert.ToInt32(Request.QueryString["id"]));
                news.Hits = nnn.Hits;
                news.StfCode = nnn.StfCode;
                news.NewsId = nnn.NewsId;
                news.NewDate = nnn.NewDate;
                new NewsManager().UpdateNews(news);
            }
            else
            {
                news.Hits = 0;
                news.NewDate = DateTime.Now;
               // news.StfCode = ((Session["username"]) as Staff).StfCode;
                news.StfCode = "admin";
                new NewsManager().AddNews(news);
            }
        }


    }
    private bool CheckEmpty()
    {
        if (this.txtTitle.Text == "")
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('新闻标题不能为空！')</script>");
            return false;
        }
        if (this.rbNo.Checked == false && this.rbYes.Checked == false)
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('您还没有选择是否推荐！')</script>");
            return false;
        }
        if (this.rbTopYes.Checked == false && this.rbTopNo.Checked == false)
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('您还没有选择是否置顶！')</script>");
            return false;
        }
        if (this.ddlNewsType.SelectedItem.Text == "请选择")
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('您还没有选择类型！')</script>");
            return false;
        }
        return true;
    }
}